/**
 * <p>Title: ServiceZMCL30.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-15 下午08:23:17  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.cl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cl.domain.ZMCLPointAssociation;
import com.baosight.sczx.zm.cl.domain.ZMCLSecurityPath;
import com.baosight.sczx.zm.cl.domain.ZMCLSurveyPoint;

/**
 * <p>Title: ServiceZMCL30.java</p>
 * <p>Description: 安全巡检点管理</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-15 下午08:23:17  张小飞  Create
 *</pre>
 */
public class ServiceZMCL30 extends ServiceEPBase {
	/**
	 * 安全巡检点管理 进入初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCL01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		String surveyPointCode = this.getSurveyPointCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdSurveypointCode");
		outInfo.set("surveyPointCode", surveyPointCode);//新增时按照规则生成编号
		
		
		return outInfo;
	}
	
	/**
	 * 安全巡检点 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-3-28              高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo)
	{
		EiInfo queryInfo = super.query(inInfo, "ZMCLSurveyPoint.query", new ZMCLSurveyPoint());
		EiInfo outInfo = getPathName(queryInfo);//获得引用的巡检路线名称
		return outInfo;
	}
	
	/**
	 * 获得巡检路线名称
	 * <p>Function: getPathName</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7   高强
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private EiInfo getPathName(EiInfo info)
	{
		EiBlock resultBlock = info.getBlock(EiConstant.resultBlock);
		for(int i=0; i<resultBlock.getRowCount(); i++)
		{
			if(info.getBlock(EiConstant.queryBlock) == null) {
				info.addBlock(EiConstant.queryBlock);
			}
			if(resultBlock.getRow(i).get("fdKeyId") !=null)
			{
				String fdKeyId = resultBlock.getRow(i).get("fdKeyId").toString();
				Map<String, String> querySecuritypathIdMap = new HashMap<String, String>();
				querySecuritypathIdMap.put("fdSurveypointId", fdKeyId);//添加查询条件
				List resultSecuritypathIdMap = dao.query("ZMCLPointAssociation.query", querySecuritypathIdMap);//获得巡检路线的主键
				StringBuffer pathName = new StringBuffer();
				if(resultSecuritypathIdMap.size()!=0)
				{
					for(int j=0; j<resultSecuritypathIdMap.size(); j++)
					{
						ZMCLPointAssociation data = (ZMCLPointAssociation)resultSecuritypathIdMap.get(j);
						Map<String, String> queryPathNameMap = new HashMap<String, String>();
						queryPathNameMap.put("fdKeyId", data.getFdSecuritypathId());//添加查询条件
						List resultMap = dao.query("ZMCLSecurityPath.query", queryPathNameMap);//获得巡检路线的主键
						for(int k=0; k<resultMap.size(); k++)
						{
							ZMCLSecurityPath data1 = (ZMCLSecurityPath)resultMap.get(k);
							if(j == resultSecuritypathIdMap.size()-1)
							{
								pathName.append(data1.getFdPathName());//如果是引用了一个巡检路线或者是最后一个巡检路线不以分号结束
								break;
							}
							pathName.append(data1.getFdPathName()+";");
						}
					}
				}
				resultBlock.getRow(i).put("fdPathName", pathName.toString());//获得巡检路线名称
			}
		}
		return info;
	}
	
	/**
	 * 查询最新的巡检点编号 
	 * <p>Function: getSurveyPointCode</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-3-28              高强       
	 * </p>
	 *
	 */
	
	public EiInfo getSurveyPointCode(EiInfo inInfo)
	{
		EiInfo Info = super.query(inInfo, "ZMCLSurveyPoint.queryLatestSurveyPointCode", new ZMCLSurveyPoint());
		EiInfo outInfo = new EiInfo();
		outInfo.addBlock(EiConstant.resultBlock).addBlockMeta(new ZMCLSurveyPoint().eiMetadata);
		if(Info.getBlock("result").getRowCount() == 0)
		{
			String defaultStr = "AQXJD" + "_" + StaticTools.getDateDefault()+ "_" + "001";//如果在当天没有记录生成，则默认为是当天的第一条记录
			outInfo.getBlock("result").setCell(0,"fdSurveypointCode", defaultStr);
		}else
		{
			String str = Info.getBlock(EiConstant.resultBlock).getCell(0, "fdSurveypointCode").toString();
			outInfo.getBlock("result").setCell(0,"fdSurveypointCode", StaticTools.convertStr(str, 15, 18));//编号的前15位AQXJD_YYYYMMDD_,全长为18位
		}
		return outInfo;
	}
	
	/**
	 * 安全巡检点 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强         
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo)
	{
		boolean validateValue = validate(inInfo, new HashMap<String, String>());//有效性验证
		if(validateValue) {
			return inInfo;
		}
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CI_SURVEYPOINT"));//主键
		inInfo.getBlock("result").setCell(0, "fdStatus", "在用");//新增状态默认为在用
		EiInfo outInfo = super.insert(inInfo, "ZMCLSurveyPoint.insert", "result");
		
		String surveyPointCode = this.getSurveyPointCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdSurveypointCode");
		outInfo.set("surveyPointCode", surveyPointCode);//新增时按照规则生成编号
		return outInfo;
	}
	
	/**
	 * 安全巡检点 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCLSurveyPoint.update");
		return outInfo;
	}
	
	/**
	 * 安全巡检点 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo)
	{
		super.update(inInfo, "ZMCLSurveyPoint.delete", "result");
		EiInfo outInfo = this.query(inInfo);
		
		String surveyPointCode = this.getSurveyPointCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdSurveypointCode");
		outInfo.set("surveyPointCode", surveyPointCode);//新增时按照规则生成编号
		return outInfo;
	}
	
	/**
	 * 检查是否有重复的巡检编号存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7下午01:32:59 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdSurveypointCode = inInfo.getBlock("result").getCellStr(0, "fdSurveypointCode");
		params.put("fdSurveypointCode", fdSurveypointCode);
		
		List result = dao.query("ZMCLSurveyPoint.query", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCLSurveyPoint _temp = (ZMCLSurveyPoint)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdSurveypointCode() != null && _temp.getFdSurveypointCode().equals(fdSurveypointCode)) {
				msg.append(" 该巡检点编号 ");
			}
			msg.append("重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 * 启用某条数据
	 * <p>Function: modifyStart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午         高强
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLSurveyPoint.modifyStart");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 停用某条数据
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午         高强
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLSurveyPoint.modifyStop");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
}
